from langchain.chains.combine_documents import create_stuff_documents_chain
from langchain_community.chat_models import ChatTongyi
from langchain_community.document_loaders import WebBaseLoader
from langchain_core.prompts import ChatPromptTemplate
from pydantic import SecretStr


headers = {
    "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/135.0.0.0 Safari/537.36"
}

# 网页加载器，加载网页内容
# bs4_strainer = bs4.SoupStrainer(class_=("post-title", "post-header", "post-content"))
loader = WebBaseLoader(
    web_paths=("https://liaoxuefeng.com/books/python/history/index.html",),
    header_template=headers,
    # bs_kwargs={"parse_only": bs4_strainer},
)
docs = loader.load()


chatLLM = ChatTongyi(
    model="qwen-plus-2025-04-28",   # 此处以qwen-max为例，您可按需更换模型名称。模型列表：https://help.aliyun.com/zh/model-studio/getting-started/models
    streaming=True,
    api_key = SecretStr("sk-d16b46d66abb45bb960bd9c57804e2f9"),
    # other params...
)

# Define prompt
prompt = ChatPromptTemplate.from_messages(
    [("system", "撰写以下内容的简明摘要:\\n\\n{context}")]
)

# Instantiate chain
chain = create_stuff_documents_chain(chatLLM, prompt)

# Invoke chain
# result = chain.invoke({"context": docs})
# print(result)

for token in chain.stream({"context": docs}):
    print(token, end="|")